package cn.com.backstage.controller;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.com.api.entity.ApiT_city;
import cn.com.api.entity.ApiT_coupon_act;
import cn.com.api.entity.ApiT_points_rule;
import cn.com.api.mapper.ApiT_coupon_actMapper;
import cn.com.api.service.ApiT_coupon_actService;
import cn.com.api.service.StartOnLoadService;
import cn.com.util.*;
import cn.com.util.*;
import cn.com.util.*;
import cn.com.util.*;

@Controller
@RequestMapping(Utils.SYS_BACKSTAGE+"/")
public class T_coupon_actController{

	private static Logger logger = Logger.getLogger(T_coupon_actController.class);
	
	@Autowired
	private StartOnLoadService startOnLoadService;
	@Autowired
	private ApiT_coupon_actService apiT_coupon_actService;
	@Autowired
	private ApiT_coupon_actMapper apiT_coupon_actMapper;
	
	@RequestMapping(value = "coupon_act_list")
	public ModelAndView coupon_act_list(HttpServletRequest request,
			HttpServletResponse response,PageInfo pageInfo) throws Exception{
		ModelAndView mv = new ModelAndView("forward:/backstage/t_coupon_act_list.jsp");
		String city_id = request.getParameter("city_id");
		Map<String,Object> conditions = new HashMap<String,Object>();
		conditions.put("city_id", city_id);
		
		List<ApiT_coupon_act> couponActList = apiT_coupon_actService.searchCouponActList(conditions, pageInfo);
		mv.addObject("list", couponActList);
		mv.addObject("pageInfo", pageInfo);
		mv.addObject("conditions", conditions);
		List<ApiT_city> citys = startOnLoadService.citys();
		mv.addObject("citys", citys);
		return mv;
	} 
	
	@RequestMapping(value = "coupon_act_add_pre")
	public ModelAndView coupon_act_add_pre(HttpServletRequest request,
			HttpServletResponse response) throws Exception{
		ModelAndView mv = new ModelAndView("forward:/backstage/t_coupon_act_add.jsp");
		
		List<ApiT_city> citys = startOnLoadService.citys();
		mv.addObject("citys", citys);
		mv.addObject("inviteSize",0);
		mv.addObject("being_inviteSize",0);
		return mv;
	} 
	
	@RequestMapping(value = "coupon_act_addOrUpdate_pre")
	public ModelAndView coupon_act_addOrUpdate_pre(HttpServletRequest request,
			HttpServletResponse response) throws Exception{
		ModelAndView mv = new ModelAndView("forward:/backstage/t_coupon_act_add.jsp");
		
		String id = request.getParameter("id");
		ApiT_coupon_act t_coupon_act = apiT_coupon_actService.searchCouponActById(id);
		
		
		List<ApiT_city> citys = startOnLoadService.citys();
		mv.addObject("citys", citys);
		mv.addObject("t_coupon_act", t_coupon_act);
		Integer inviteSize = 0;
		Integer being_inviteSize = 0;
		if(t_coupon_act!=null){
			inviteSize = t_coupon_act.getInviteList().size();
			being_inviteSize = t_coupon_act.getBeing_invitedList().size();
		}
		mv.addObject("inviteSize", inviteSize);
		mv.addObject("being_inviteSize", being_inviteSize);
		return mv;
	} 
	
	@RequestMapping(value = "coupon_act_addOrUpdate")
	public void coupon_act_addOrUpdate(HttpServletRequest request,
			HttpServletResponse response,ApiT_coupon_act t_coupon_act) throws Exception{
		Map<String, Object> map = new HashMap<String, Object>();
		try{
			
			//添加前校验-city_id act_type 联合查询是否已添加过
			
			if(!apiT_coupon_actService.checkExistCouponAct(t_coupon_act)){
				t_coupon_act.setStatus(1);
				apiT_coupon_actService.addOrUpdateCouponAct(t_coupon_act);
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "操作成功！");
			}else{
				map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
				map.put(Utils.MESSAGE, "该城市推荐有奖已添加过！");
			}
			
			
		}catch(Exception e){
			e.printStackTrace();
			logger.error(Util.getStackTraceAsString(e));
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.write(JsonUtil.object2json(map));
		System.out.println(JsonUtil.object2json(map));
		out.close();
		
	} 
	
	@RequestMapping(value = "coupon_act_delete")
	public void coupon_act_delete(HttpServletRequest request,
			HttpServletResponse response) throws Exception{
		Map<String, Object> map = new HashMap<String, Object>();
		try{
			String id = request.getParameter("id");
			apiT_coupon_actService.deleteCouponActById(id);
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "删除成功！");
		}catch(Exception e){
			e.printStackTrace();
			logger.error(Util.getStackTraceAsString(e));
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.write(JsonUtil.object2json(map));
		out.close();
		
	} 
	
}



